package com.app.action;

import java.io.File;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.app.entity.Product;
import com.app.entity.Series;
import com.app.service.ProductService;
import com.app.service.SeriesService;
import com.app.utils.Struts2Utils;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller
public class ProductAction extends BaseActionSupport<Product> {
	private Product product = new Product();

	@Autowired
	private ProductService productService;
	
	@Autowired
	private SeriesService seriesService;
	
	private Collection<Series> seriesList;
	
	private Collection<Series> seriesNameList;
	
	private Collection<Product> productList;

	private File[] doc;
	private String[] docContentType;
	private String[] docFileName;
	
	private String idStr;
	private int value;

	public String list(){
		productList = productService.findProductByParameter(product);
		return SUCCESS;
	}
	
	public String saveOrUpdate() throws Exception {
		productService.saveOrUpdateProduct(product, doc, docFileName, docContentType);
		return "reload";
	}
	
	public String delete() throws Exception {
		productService.deleteProduct(product.getSpec());
		return list();
	}
	
	public void updateOnlie() throws Exception{
		int num = productService.updateOnline(idStr, value);
		Struts2Utils.renderText(String.valueOf(num));
	}

	protected void prepareModel() throws Exception {
		if(id != null)
			product = productService.load(id);
		//else
			//product = new Product();
	}

	public Product getModel() {
		return product;
	}

	public File[] getDoc() {
		return doc;
	}

	public void setDoc(File[] doc) {
		this.doc = doc;
	}

	public String[] getDocContentType() {
		return docContentType;
	}

	public void setDocContentType(String[] docContentType) {
		this.docContentType = docContentType;
	}

	public String[] getDocFileName() {
		return docFileName;
	}

	public void setDocFileName(String[] docFileName) {
		this.docFileName = docFileName;
	}
	
	public Collection<Series> getSeriesList() {
		Series series = new Series();
		series.setFlag(1);
		seriesList = seriesService.findSeriesByParameter(series);
		return seriesList;
	}

	public void setSeriesList(Collection<Series> seriesList) {
		this.seriesList = seriesList;
	}

	public Collection<Series> getSeriesNameList() {
		Series series = new Series();
		series.setFlag(2);
		seriesNameList = seriesService.findSeriesByParameter(series);
		return seriesNameList;
	}

	public void setSeriesNameList(Collection<Series> seriesNameList) {
		this.seriesNameList = seriesNameList;
	}

	public Collection<Product> getProductList() {
		return productList;
	}

	public void setProductList(Collection<Product> productList) {
		this.productList = productList;
	}

	public String getIdStr() {
		return idStr;
	}

	public void setIdStr(String idStr) {
		this.idStr = idStr;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}
}
